import { _decorator, Component, Node, Game, Vec3, Quat, Prefab, ParticleSystem } from 'cc';
import { poolManager } from '../../../Framework/Scripts/Managers/poolManager';
import myUtil from '../../../Framework/Scripts/Utils/myUtil';
import { Vector3 } from '../../../Framework/Scripts/Utils/Vector3';
import { OBJ_TYPE } from '../constant';
import { GameApp } from '../GameApp';
import { bulletGun } from './bulletGun';
import { enemyBase } from './enemyBase';
const { ccclass, property } = _decorator;
//普通机枪炮塔
@ccclass('towerGun')
export class towerGun extends enemyBase {
    @property({ type: Prefab, displayName: "子弹" })
    private bullet: Prefab = null

    @property({ type: Number, displayName: "速度" })
    private bulletSpeed: number = 100
    @property({ type: Number, displayName: "范围" })
    private range: number = 50
    private nowQuat: Quat = new Quat()
    private dstRot: Quat = new Quat()
    


    private time: number = 0.2
    private timeAdd: number = 0
    private enemyArr: Node = null
    start() {

    }
    init() {

    }
    private fire(target: Node, dt: number) {
        if (!target) {
            return
        }
        const sub: Vec3 = Vector3.sub(target.getWorldPosition(), this.node.getWorldPosition())
        const len: number = sub.lengthSqr()
        const dir: Vec3 = sub.normalize()

        if (len <= this.range) {

            if (this.timeAdd < this.time) {
                this.timeAdd += dt
            } else {
                this.timeAdd = 0
                const fire: Node = this.node.getChildByName("fire")
            
                
                this.Playfire(fire)

                const bullet: Node = poolManager.instance.getNode(this.bullet, this.node.parent)
                const ts: bulletGun = bullet.getComponent(bulletGun)
                if (ts) {
                    ts.init(this.node, target, this.bulletSpeed, dir, this.node.getChildByName("startPos").getWorldPosition(), len)
                }
            }


            var old: Quat = this.node.getRotation();
            this.node.forward = dir
            this.dstRot = this.node.getRotation();
            this.node.setRotation(old);
            Quat.slerp(this.nowQuat, this.node.getRotation(), this.dstRot, 10.0 * dt);
            this.node.setRotation(this.nowQuat);


        }

    }

    private Playfire(node: Node) {
        const arr = node.getComponentsInChildren(ParticleSystem);
        for (let a of arr) {
            a.stop();
            a.play();
        }
    }
    onLoad() {
        this.range *= this.range//转换平方
        this.enemyArr = GameApp.Instance.enemyRoot
        this.obj_type = OBJ_TYPE.friendly

    }
    private findTarget(): Node {
        //找敌人


        var nodeArr = []
        const enemyArr = this.enemyArr.children

        for (let i = 0; i < enemyArr.length; i++) {
            const len: number = myUtil.getTwoNodeXZLengthSqr(enemyArr[i], this.node)

            if (len <= this.range) {
                nodeArr.push(enemyArr[i])

            }
        }


        return this.getNearestMonster(nodeArr)




    }
    protected getNearestMonster(enemyArr: Node[]) {


        if (enemyArr.length) {
            let arr = enemyArr.sort((a: any, b: any) => {
                let distanceA = myUtil.getTwoNodeXZLengthSqr(this.node, a);
                let distanceB = myUtil.getTwoNodeXZLengthSqr(this.node, b);
                return distanceA - distanceB;
            })

            arr = arr.filter((item: Node) => {
                let ts: enemyBase = null

                ts = item.getComponent(enemyBase);



                return item.parent !== null && ts && !ts.isDie
            })

            return arr[0];
        } else {
            return null;
        }
    }
    update(dt: number) {


        this.fire(this.findTarget(), dt)


    }
}


